<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<!--edge浏览器H5兼容设置-->
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<!--360浏览器H5兼容设置-->
		<meta name="renderer" content="webkit" />
		<title>登录</title>
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<!--导入核心文件-->
		<script src="../bootstrap3/js/holder.js"></script>
		<link href="../bootstrap3/css/bootstrap.css" rel="stylesheet" type="text/css">
		<script src="../bootstrap3/jquery-1.9.1.min.js"></script>
		<script src="../bootstrap3/js/bootstrap.js"></script>
		<!-- 字体图标 -->
		<link rel="stylesheet" href="../bootstrap3/font-awesome-4.7.0/css/font-awesome.css" />
		<link rel="stylesheet" type="text/css" href="../css/top.css" />
		<link rel="stylesheet" type="text/css" href="../css/index.css" />
		<link rel="stylesheet" type="text/css" href="../css/layout.css" />
		<link rel="stylesheet" type="text/css" href="../css/login.css" />
		<link rel="stylesheet" type="text/css" href="../css/footer.css" />
		<link rel="shortcut icon" href="../favicon.ico"/>
		<script src="../bootstrap3/js/jquery.cookie.js" type="text/javascript" charset="utf-8"></script>
		<script src="../js/autoLogin.js" type="text/javascript"></script>
	</head>
	<body>
		<!--主体-->
		<div class="container mycontent text-left">
			<!--透明层 -->
			<div class="loginDiv">
				<p>用户登录</p>
				<!--表单开始-->
				<form id="form-login" action="index.html"  class="form-horizontal" role="form">
					<!--用户名-->
					<div class="form-group">
						<label for="username" class="col-md-3 control-label">用户名：</label>
						<div class="col-md-8">
							<input name="username" type="text" class="form-control" id="username"  placeholder="请输入用户名">
						</div>
					</div>
					<!--密码-->
					<div class="form-group">
						<label for="password" class="col-md-3 control-label"> 密码：</label>
						<div class="col-md-8">
							<input name="password" type="text" class="form-control" id="password"  placeholder="请输入密码">
						</div>
					</div>
					<!--验证码-->
					<div class="form-group">
						<label for="verification" class="col-md-3 control-label"> 验证码：</label>
						<div class="col-md-8">
							<input name="verification" type="text" class="form-control" id="verification"  placeholder="请输入验证码">
							<img name="imgCode" id="imgCode" ref="vcImg" src="/login/getImage" alt="验证码">
						</div>
					</div>
					<!-- 记住我-->
					<div class="form-group">
						<div class="col-md-offset-3 col-md-6">
							<div class="checkbox">
								<label>
									<input type="checkbox" id="auto">自动登录
								</label>
							</div>
						</div>
					</div>
					<!--提交按钮-->
					<div class="form-group">
						<label class="col-md-3 control-label"></label>
						<div class="col-md-8">
							<input id="btn-login" class="btn btn-primary" type="button" value="登录" />
							<span class="pull-right"><small>还没有账号？</small><a href="register.html">注册</a></span>
						</div>
					</div>
				</form>
			</div>
		</div>
		<script type="text/javascript">
			// 重新获取表单验证码
			$("#imgCode").click(function (){
				// 直接通过src去请求验证码图片 通过Math.random()防止缓存问题
				$("#imgCode").attr("src","/login/getImage?"+Math.random());
			});

			/*$("#password").keydown(enter);
			function enter(e){
				if(e.keyCode == 13){
					$("#btn-login").click();
				}
			}*/

			$("#password").keydown(function (e){
				if(e.keyCode == 13){
					$("#btn-login").click();
				}
			});

			$("#btn-login").click(function() {
				$.ajax({
					url: "/login/login",
					type: "POST",
					data: $("#form-login").serialize(),
					dataType: "json",
					sync: false,
					success: function(json) {
						if (json.state == 200) {
							//如果有原url
							if (json.data.avatar){
								location.href = json.data.avatar;
								return;
							}
							switch (json.data.level){
								case "00":
									location.href = "adminIndex.html";
									break;
								case "99":
									alert("当前浏览器已经有用户登录系统！请先把原登录账户注销登录，或换一个浏览器登录你的账号！");
									location.href = "logout.html";
									break;
								default:
									location.href = "index.html";
									break;
							}
						} else {
							$("#imgCode").attr("src","/login/getImage?"+Math.random());
							alert("登录失败！" + json.message);
						}
					},
					error:function(xhr){
						alert("登录请求异常！"+xhr.message);
					}
				});
			});
		</script>
	</body>
</html>